<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="700px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" label-width="170px" label-suffix=":" v-loading="loading">
            <el-form-item label="页面类型" prop="page_type">
                <el-select v-model="model.page_type" placeholder="请选择页面类型" clearable filterable>
                    <el-option v-for="(dict, index) in pageTypeList" :key="index" :label="dict.label" :value="dict.label"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="访问路径" prop="page_path">
                <el-input v-model="model.page_path" placeholder="请输入访问路径" clearable></el-input>
            </el-form-item>
            <el-form-item label="商品spu" prop="spu" v-if="model.page_type !== 'Product'">
                <el-input v-model="model.spu" placeholder="请输入商品spu" clearable></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { add_page_path } from '@/api/report'
export default {
    name: 'DetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
        pageTypeList: {
            type: Array,
            default: () => [],
        },
    },
    data() {
        return {
            loading: false,
            save: false,
            model: {
                page_type: undefined,
                page_path: undefined,
                spu: undefined,
            },
            rules: {
                page_type: [{ required: true, message: '页面类型不能为空', trigger: 'blur' }],
                page_path: [{ required: true, message: '访问路径不能为空', trigger: 'blur' }],
            },
            salesList: [],
        }
    },
    computed: {
        title() {
            return `新增`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },

    created() {},
    methods: {
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }

                const params = { ...this.model }
                if (this.save) {
                    return
                }
                this.save = true
                add_page_path(params)
                    .then(res => {
                        this.$message.success(res.msg)
                        this.dialogVisible = false
                        this.$emit('success')
                    })
                    .finally(() => {
                        this.save = false
                    })
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style scoped>
.el-form-item {
    padding-right: 100px;
}
.el-form-item .el-select {
    width: 100%;
}
</style>
